<?php

$server = new Swoole\Websocket\Server('0.0.0.0', 9502);

$server->on('start', function ($server) {
    echo "Websocket Server is started\n";
});

$server->on('open', function ($server, $request) {

    $getArr = $request->get;
    $uid = $getArr['uid'];

    $fd = $request->fd;
    $server->bind($fd, $uid);
    $clientInfo = $server->getClientInfo($fd);

    var_dump('fd为：' . $fd);
    var_dump('uid为：' . $clientInfo['uid']);

});

$server->on('message', function ($server, $frame) {
    echo "received message: {$frame->data}\n";

    // 记录聊天内容
    $dataObj = \app\index\controller\MemberGroupsChat::add($frame->data);
    $ndataObj = json_decode($frame->data);
    foreach ($server->connections as $fd) {
        var_dump($fd);
        $clientInfo = $server->getClientInfo($fd);
        var_dump($clientInfo['uid']);
        var_dump($ndataObj->fromUserId);
        $dataObj->meFlag = $clientInfo['uid'] == $ndataObj->fromUserId ? true : false;
        var_dump($dataObj);
        $server->push($fd, $dataObj);
    }
});

$server->on('close', function ($server, $fd) {
    echo "connection close: {$fd}\n";
});

$server->start();
